/*
* @Author: viivLgr
* @Date:   2018-03-20 10:12:45
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-21 23:35:50
*/

'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide = require('page/common/nav-side/index.js');
var _util = require('util/util.js');
var _user = require('service/user-service.js');

// page逻辑部分
var page = {
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        // 初始化左侧菜单
        navSide.init({
            name: 'user-pass-update'
        });
        // 加载用户信息
        this.loadUserInfo();
    },
    loadUserInfo: function () {
       
    },
    bindEvent: function(){
        var _this = this;
        $(document).on('click', '.btn-submit', function(){
            var userInfo = {
                    password: $.trim($('#password').val()),
                    passwordNew: $.trim($('#password-new').val()),
                    passwordConfirm: $.trim($('#password-confirm').val())
                },
            validateResult = _this.validateForm(userInfo);
            if(validateResult.status){
                _user.updatePassword({
                    passwordOld: userInfo.password,
                    passwordNew: userInfo.passwordNew
                }, function(res, msg){
                    _util.successTips(msg);
                }, function(err){
                    _util.errorTips(err);
                });
            }else{
                _util.errorTips(validateResult.msg);
            }
        });
    },
    validateForm: function(formData){
        var result = {
            status: false,
            msg: ''
        };
        // 验证原密码不能为空
        if(!_util.validate(formData.password, 'require')){
            result.msg = '原密码不能为空';
            return result;
        }
        // 验证原密码不能为空
        if(!formData.passwordNew || formData.passwordNew.length < 6){
            result.msg = '新密码长度不得小于6位';
            return result;
        }
        // 两次输入密码是否一致
        if(formData.passwordNew !== formData.passwordConfirm){
            result.msg = '两次输入的密码不一致';
            return result;
        }
        // 通过验证，返回正确提示
        result.status = true;
        result.msg = '验证通过';
        return result;
    }
};
$(function () {
    page.init();
});